package cn.net.isir.controller.application;

import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.core.IgnoredTicket;
import cn.net.isir.core.ShiroUsernamePasswordPhoneToken;
import cn.net.isir.entity.ShopUser;
import cn.net.isir.entity.dto.LoginUser;
import cn.net.isir.service2.ShopService;
import cn.net.isir.service2.ShopUserService;
import cn.net.isir.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuxubo
 * @filename LoginController.java
 * @package cn.net.isir.controller.application
 * @email lxb@isir.net.cn
 * @date 2019/01/02
 * @comment
 * @since 1.8
 */
@RestController
@RequestMapping("/app/login")
public class LoginController {

    @Resource
    private ShopUserService shopUserService;
    @Resource
    private ShopService shopService;

    @IgnoredTicket
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultObject login(@RequestBody LoginUser loginUser) {
        if (StringUtils.isBlank(loginUser.getUserName())) {
            return new ResultObject(500, "fail", null);
        }
        if (StringUtils.isBlank(loginUser.getPassword())) {
            return new ResultObject(500, "fail", null);
        }
        ResultObject ro = new ResultObject();
        try {
            //尝试登陆
//            UsernamePasswordToken upToken = new UsernamePasswordToken(loginUser.getUserName(), loginUser.getPassword());
            ShiroUsernamePasswordPhoneToken phoneToken =
                    new ShiroUsernamePasswordPhoneToken(loginUser.getUserName(), loginUser.getPassword());
            SecurityUtils.getSubject().login(phoneToken);
        } catch (UnknownAccountException ex) {
            ex.printStackTrace();
            ro.setResult(500);
            ro.setMessage("用户不存在");
            return ro;
        } catch (IncorrectCredentialsException ex) {
            ex.printStackTrace();
            ro.setResult(500);
            ro.setMessage("用户名或密码不匹配");
            return ro;
        } catch (LockedAccountException ex) {
            ex.printStackTrace();
            ro.setResult(500);
            ro.setMessage("账户已锁定");
            return ro;
        } catch (DisabledAccountException ex) {
            ex.printStackTrace();
            ro.setResult(500);
            ro.setMessage("账户已失效");
            return ro;
        } catch (AuthenticationException ex) {
            ex.printStackTrace();
            ro.setResult(500);
            ro.setMessage("其他错误");
            return ro;
        }
//        logger.info(subject.getPrincipal());
//        String userName = (String) subject.getPrincipal();

        ro.setResult(200);
        ro.setMessage("success");
        //查询店铺列表
        ShopUser shopUser = new ShopUser();
        shopUser.setUserId(TokenManager.getTokenId());
        List<ShopUser> shopUserList = shopUserService.selectList(shopUser);
        for (ShopUser su : shopUserList) {
            su.setShopName(shopService.selectByPrimaryKey(su.getShopId()).getName());
        }
        ro.setObject(shopUserList);
        return ro;
    }
}
